package com.hopu.oa.controller.address;

import com.hopu.oa.dao.address.AddressDao;
import com.hopu.oa.entity.address.Director;
import com.hopu.oa.entity.address.DirectorUsers;
import com.hopu.oa.entity.process.AoaAttachmentList;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.until.OosUtil;
import com.hopu.oa.until.PinYinUtil;
import com.hopu.oa.until.data.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
public class AddrController {
    @Autowired
    AddressDao addressDao;

    //工具类，文件上传
    @Autowired
    private OosUtil oosUtil;


    @RequestMapping("addrmanage")
    public String addrmanage(@Param("baseKey") String baseKey,
                             @RequestParam(value = "alph", defaultValue = "") String alph, Model model,
                             @RequestParam(value = "page", defaultValue = "1") int page,
                             @RequestParam(value = "size", defaultValue = "10") int size,
                             HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        int totalElements = addressDao.selectUserByAll(baseKey, alph, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> userInfos = addressDao.selectUserByAll(baseKey, alph, (page - 1) * size, size);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("catalogs", addressDao.selectById(user.getUserId()));
        model.addAttribute("users", userInfos);
        model.addAttribute("url", "addrmanagee");
        int size1 = addressDao.selectByY(user.getUserId()).size();
        model.addAttribute("count", size1);

        return "address/addrmanage";
    }

    @RequestMapping("inaddresspaging")
    public String inaddresspaging(@Param("baseKey") String baseKey,
                                  @RequestParam(value = "alph", defaultValue = "") String alph, Model model,
                                  @RequestParam(value = "page", defaultValue = "1") int page,
                                  @RequestParam(value = "size", defaultValue = "10") int size,
                                  HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        int totalElements = addressDao.selectUserByAll(baseKey, alph, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> userInfos = addressDao.selectUserByAll(baseKey, alph, (page - 1) * size, size);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("catalogs", addressDao.selectById(user.getUserId()));
        model.addAttribute("users", userInfos);
        model.addAttribute("url", "addrmanagee");
        return "address/inaddrss";
    }

    @RequestMapping("addrmanagee")
    public String addrmanagee(@Param("baseKey") String baseKey,
                              @RequestParam(value = "alph", defaultValue = "") String alph, Model model,
                              @RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "size", defaultValue = "10") int size,
                              HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        int totalElements = addressDao.selectUserByAll(baseKey, alph, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> userInfos = addressDao.selectUserByAll(baseKey, alph, (page - 1) * size, size);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("users", userInfos);
        model.addAttribute("catalogs", addressDao.selectById(user.getUserId()));
        model.addAttribute("url", "addrmanagee");
        return "address/inaddrss";
    }

    /**
     * 查看内部联系人
     */
    @RequestMapping("inmessshow")
    public String inMessShow(Model model, int userId) {
        AoaUser one = addressDao.findOne(userId);
        model.addAttribute("user", one);
        return "address/inmessshow";
    }

    /**
     * 编辑
     *
     * @return
     */
    @RequestMapping("addaddress")
    public String addaddress(Integer userId,
                             @RequestParam(value = "did", defaultValue = "0") int did, HttpSession session, Model model) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        List<String> strings = addressDao.selectById(user.getUserId());
        if (did != 0) {
            Director director = addressDao.selectByDid(user.getUserId(), did);
            model.addAttribute("director", director);

        }
        model.addAttribute("calogs", strings);
        return "address/addressedit";
    }

    /**
     * 保存
     *
     * @param director
     * @param catelogName
     * @param session
     * @param model
     * @param file
     * @return
     */
    @RequestMapping("savaaddress")
    public String savaaddress(Director director, String catelogName, HttpSession session, Model model,
                              @RequestParam("userName") String username,
                              @RequestParam("file") MultipartFile file) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        director.setUserId(user.getUserId());


        /**
         * 添加
         */
        if (director.getDirectorId() == null) {
            Director director1 = addressDao.selectByName(director.getUserName());
            if (director1 == null) {
                if (file.getSize() != 0) {
                    // 文件上传并获取 url
                    String url = oosUtil.getImgUrl(oosUtil.uploadImg2Oss(file));
                    String shuffix = oosUtil.getPicSuffix(file.getOriginalFilename());
                    long size = file.getSize();
                    String type = file.getContentType();
                    AoaAttachmentList attachment = new AoaAttachmentList();
                    attachment.setAttachmentName(file.getOriginalFilename());
                    attachment.setAttachmentPath(url);
                    attachment.setAttachmentSize(size + "");
                    attachment.setAttachmentType(type);
                    attachment.setAttachmentShuffix(shuffix);
                    attachment.setModel("note");
                    attachment.setUploadTime(DateUtils.getDateTimeString());
                    attachment.setUserId(user.getUserId() + "");
                    addressDao.inertAttachment(attachment);

                    director.setImagePath(attachment.getAttachmentId());
                    director.setPinyin(PinYinUtil.getPinyin(username));
                    int i = addressDao.addDirector(director);

                    addressDao.adddirectorUsers(catelogName, 0, director.getDirectorId(), user.getUserId());

                    return "redirect:/addrmanage";
                } else {
                    director.setImagePath(null);
                    director.setPinyin(PinYinUtil.getPinyin(username));
                    int i = addressDao.addDirector(director);

                    addressDao.adddirectorUsers(catelogName, 0, director.getDirectorId(), user.getUserId());

                    return "redirect:/addrmanage";
                }

            }
        }


        /**
         * 修改
         */
        else {

            String userName = addressDao.selectDirectorById(director.getDirectorId()).getUserName();

            if (director.getUserName().equals(userName)) {
                director.setPinyin(PinYinUtil.getPinyin(username));
                addressDao.updateDirectorById(director);
                addressDao.updateDirectorUsersById(catelogName, director.getDirectorId(), user.getUserId());
                return "redirect:/addrmanage";
            } else {
                Director director1 = addressDao.selectByName(director.getUserName());
                if (director1 == null) {
                    director.setPinyin(PinYinUtil.getPinyin(username));
                    addressDao.updateDirectorById(director);
                    addressDao.updateDirectorUsersById(catelogName, director.getDirectorId(), user.getUserId());
                    return "redirect:/addrmanage";
                } else {

                }
            }
        }
        List<String> strings = addressDao.selectById(user.getUserId());
        model.addAttribute("calogs", strings);
        model.addAttribute("director", director);
        model.addAttribute("errormess", "这个人名已存在哦");
        return "redirect:/addrmanage";
    }

    /**
     * 添加分类
     */
    @RequestMapping("addtypename")
    public void addtypename(@RequestParam(value = "typename", required = false) String typename,
                            @RequestParam(value = "oldtypename", required = false) String oldtypename,
                            HttpSession session, Model model) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        DirectorUsers directorUsers = addressDao.selectBYtypename(typename);


        if ((typename != "" && directorUsers == null) && oldtypename == null) {

            addressDao.addTypeName(typename, user.getUserId());
        }

        if (typename.equals(oldtypename)) {
            addressDao.updateDirectorUsersqById(typename, user.getUserId(), oldtypename);
        } else {
            if ((typename != "" && directorUsers == null) && oldtypename != null) {
                addressDao.updateDirectorUsersqById(typename, user.getUserId(), oldtypename);
            }
        }

    }


    /**
     * 删除分类
     */
    @RequestMapping("deletetypename")
    public void deletetypename(String typename) {
        addressDao.updateTypename(typename);
    }

    /**
     * 外部通讯录
     *
     * @return
     */
    @RequestMapping("outaddresspaging")
    public String outaddresspaging(HttpSession session, Model model,
                                   @Param("baseKey") String baseKey,
                                   @RequestParam(value = "alph", defaultValue = "") String alph,
                                   @RequestParam(value = "outtype", defaultValue = "") String outtype,
                                   @RequestParam(value = "page", defaultValue = "1") int page,
                                   @RequestParam(value = "size", defaultValue = "5") int size) {
        AoaUser user = (AoaUser) session.getAttribute("user");

        int totalElements = addressDao.selectOutById(user.getUserId(), baseKey, alph, outtype, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<Director> directors = addressDao.selectOutById(user.getUserId(), baseKey, alph, outtype, (page - 1) * size, size);


        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("url", "outaddresspagingg");
        model.addAttribute("directors", directors);
        model.addAttribute("userId", user.getUserId());
        return "address/outaddrss";
    }

    @RequestMapping("outaddresspagingg")
    public String outaddresspagingg(HttpSession session, Model model,
                                    @Param("baseKey") String baseKey,
                                    @RequestParam(value = "alph", defaultValue = "") String alph,
                                    @RequestParam(value = "outtype", defaultValue = "") String outtype,
                                    @RequestParam(value = "page", defaultValue = "1") int page,
                                    @RequestParam(value = "size", defaultValue = "5") int size) {
        AoaUser user = (AoaUser) session.getAttribute("user");

        int totalElements = addressDao.selectOutById(user.getUserId(), baseKey, alph, outtype, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<Director> directors = addressDao.selectOutById(user.getUserId(), baseKey, alph, outtype, (page - 1) * size, size);
        HashMap<String, Integer> map = new HashMap<>();

        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("url", "outaddresspagingg");
        model.addAttribute("directors", directors);
        model.addAttribute("userId", user.getUserId());
        return "address/outaddrss";
    }

    /**
     * 移动
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("changethistype")
    public @ResponseBody
    Boolean changethistype(int did, String catalog, HttpSession session, Model model) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        addressDao.updateDirectorUsersById(catalog, did, user.getUserId());
        return true;
    }

    @RequestMapping("outmessshow")
    public String outmessshow(Model model, int director, HttpSession session) {

        AoaUser user = (AoaUser) session.getAttribute("user");
        Director director1 = addressDao.selectDirectorById(director);
        AoaUser one = addressDao.findOne(user.getUserId());
        model.addAttribute("d", director1);
        model.addAttribute("one", one);
        return "address/outmessshow";
    }

    /**
     * 外部联系人删除
     */
    @RequestMapping("deletedirector")
    public String deletedirector(int did, Integer impath, Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        int id = addressDao.selectDirectorIdByUid(did, user.getUserId());
        addressDao.deletedirectorusersByDid(did);
        if (impath != null) {
            addressDao.deleteattachmentByDid(impath);
        }

        return "redirect:/outaddresspaging";
    }

    /**
     * 我分享的消息界面
     */
    @RequestMapping("mesharemess")
    public String meShareMess(@RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "size", defaultValue = "8") int size,
                              @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                              Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");


        int totalElements = addressDao.getMeShareMess(user.getUserId(), baseKey, page, 0).size();

        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<DirectorUsers> directors = addressDao.getMeShareMess(user.getUserId(), baseKey, (page - 1) * size, size);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("dus", directors);
        model.addAttribute("url", "semesharemess");
        return "address/mesharemess";
    }


    @RequestMapping("semesharemess")
    public String semeShareMess(@RequestParam(value = "page", defaultValue = "1") int page,
                                @RequestParam(value = "size", defaultValue = "8") int size,
                                @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                                Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");


        int totalElements = addressDao.getMeShareMess(user.getUserId(), baseKey, page, 0).size();

        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<DirectorUsers> directors = addressDao.getMeShareMess(user.getUserId(), baseKey, (page - 1) * size, size);

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("dus", directors);
        model.addAttribute("url", "semesharemess");
        return "address/mesharemess";
    }


    /**
     * sharemess
     */
    @RequestMapping("sharemess")
    public String sharemess(@RequestParam(value = "page", defaultValue = "1") int page,
                            @RequestParam(value = "size", defaultValue = "5") int size,
                            @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                            @RequestParam(value = "catalog", defaultValue = "") String catalog,
                            @RequestParam(value = "duid", defaultValue = "0") int duid,
                            Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");

        List<Director> directors = addressDao.getShareMeMess(user.getUserId(), baseKey, (page - 1) * size, size);

        int totalElements = addressDao.getShareMeMess(user.getUserId(), baseKey, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("dus", directors);
        model.addAttribute("url", "shareuser");
        List<String> strings = addressDao.selectById(user.getUserId());
        model.addAttribute("catalogs", strings);
        if (duid != 0) {
            addressDao.update(catalog, duid);
        }
        return "address/sharemess";
    }

    /**
     * sharemess
     */
    @RequestMapping("shareuser")
    public String shareuser(@RequestParam(value = "page", defaultValue = "1") int page,
                            @RequestParam(value = "size", defaultValue = "5") int size,
                            @RequestParam(value = "baseKey", defaultValue = "") String baseKey,

                            Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");


        List<Director> directors = addressDao.getShareMeMess(user.getUserId(), baseKey, (page - 1) * size, size);
        int totalElements = addressDao.getShareMeMess(user.getUserId(), baseKey, page, 0).size();

        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", map);
        model.addAttribute("dus", directors);
        model.addAttribute("url", "shareuser");

        return "address/sharemess";
    }

    /**
     * 进入分享联系人的模态框分页
     */
    @RequestMapping("modalshare")
    public String modalShare(@RequestParam(value = "page", defaultValue = "1") int page,
                             Model model,
                             @RequestParam(value = "alph", defaultValue = "") String alph,

                             @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                             @RequestParam(value = "size", defaultValue = "10") int size,
                             HttpSession session) {


        AoaUser user = (AoaUser) session.getAttribute("user");
        int totalElements = addressDao.selectUserByAll(baseKey, alph, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> userInfos = addressDao.selectUserByAll(baseKey, alph, (page - 1) * size, size);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("users", userInfos);
        model.addAttribute("url", "modalsharee");
        return "address/modalshare";
    }

    /**
     * 点击分享
     */
    @RequestMapping("shareother")
    public String shareother(@RequestParam(value = "sharedirector") int sharedirector, @RequestParam(value = "directors[]") int[] directors, Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");

        for (int i = 0; i < directors.length; i++) {
            DirectorUsers directorUsers = addressDao.selectShareMess(sharedirector, directors[i]);
            if (directorUsers == null) {
                addressDao.insertShareMess(0, sharedirector, directors[i], user.getUserId(), DateUtils.getDateTimeString());
                return "address/sharesuccess";
            }
        }
        return "address/sharesuccess1";
    }

    @RequestMapping("modalsharee")
    public String modalSharee(@RequestParam(value = "page", defaultValue = "1") int page,
                              Model model,
                              @RequestParam(value = "alph", defaultValue = "") String alph,
                              @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                              @RequestParam(value = "size", defaultValue = "10") int size,
                              HttpSession session) {


        AoaUser user = (AoaUser) session.getAttribute("user");
        int totalElements = addressDao.selectUserByAll(baseKey, alph, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> userInfos = addressDao.selectUserByAll(baseKey, alph, (page - 1) * size, size);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("users", userInfos);
        model.addAttribute("url", "modalsharee");
        return "address/shareuser";
    }


}
